/*
 * Created by JFormDesigner on Fri Jun 28 14:33:59 CST 2024
 */

package scct.cuixinlong.ui;

import java.awt.event.*;

import scct.cuixinlong.dao.TypeDao;
import scct.cuixinlong.model.GoodsType;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Lenovo
 */
public class PanelType extends JPanel {
    FormMain main;
    DefaultTableModel model;

    public PanelType(FormMain formMain) {
        initComponents();
        this.main = formMain;
        initView();
    }

    private void initView() {
        model = (DefaultTableModel) tabType.getModel();
        model.addColumn("id");
        model.addColumn("类别名称");
        model.addColumn("类别说明");
        getTable();
    }

    private void getTable() {
        model.setRowCount(0);
        TypeDao typeDao = new TypeDao();
        List<GoodsType> allTyps = typeDao.getAllType();
        for (GoodsType allTyp : allTyps) {
            model.addRow(new Object[]{allTyp.getId(), allTyp.getName(), allTyp.getRemark()});
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgAddType dlgAddType = new DlgAddType(main);
        dlgAddType.setModal(true);
        dlgAddType.setVisible(true);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - cuixinlong
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabType = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new
        javax. swing. border. EmptyBorder( 0, 0, 0, 0) , "JFor\u006dDesi\u0067ner \u0045valu\u0061tion", javax
        . swing. border. TitledBorder. CENTER, javax. swing. border. TitledBorder. BOTTOM, new java
        .awt .Font ("Dia\u006cog" ,java .awt .Font .BOLD ,12 ), java. awt
        . Color. red) , getBorder( )) );  addPropertyChangeListener (new java. beans.
        PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("bord\u0065r" .
        equals (e .getPropertyName () )) throw new RuntimeException( ); }} );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e\u7c7b\u522b");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u7c7b\u522b");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabType);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - cuixinlong
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabType;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
